<template>
	<view>
		<view v-if="flag==true">
			<view class="logo">
				<image class="img" src="../../static/logo/logo.png" mode="widthFix"></image>
			</view>
			<view class="name">欢迎登录VS</view>
			<view class="input">
				<input type="text" placeholder="请输入账号" name="mobile" v-model="mobile" />
			</view>
			<view class="input pwd">
				<input name="password" :password="show" placeholder="请输入密码" v-model="password" />
				<image class="clear" src="../../static/logo/clear.png" mode="widthFix"  @tap="password=''"></image>
				<image class="show" :src="show?'../../static/logo/unshow.png':'../../static/logo/show.png'" mode="widthFix" @click="show=!show"></image>
				<text>|</text>
				<text class="forget" @click="findpad">忘记密码</text>
			</view>
			<view class="login" @click="plogin">登录</view>
			<view class="login_style">
				<text @click="msgLogin">短信验证码登录</text>
				<text @click="goRegister">新用户注册</text>
			</view>
		</view>
		<view v-if="flag==false">
			<view class="logo">
				<image class="img" src="../../static/logo/logo.png" mode="widthFix"></image>
			</view>
			<view class="name">欢迎登录VS</view>
			<view class="input">
				<input type="text" placeholder="请输入手机号" name="mobile" v-model="mobile" />
			</view>
			<view class="input pwd">
				<input type="number" placeholder="请输入验证码" v-model="ma"/>
				<view class="gain_code" @click="getCode">{{code}}</view>
			</view>
			<view class="login" @click="ylogin">登录</view>
			<view class="login_style">
				<text @click="pwdLogin">账号密码登录</text>
				<text @click="goRegister">新用户注册</text>
			</view>
		</view>
		<!-- #ifdef H5 -->
		<view class="down" @click="appdown">APP下载</view>
		<!-- #endif -->
	</view>
</template>

<script>
	import {mligin,ylogin,getsms,mybi} from "../../api/login.js"
	export default {
		data() {
			return {
				mobile:"",
				password:"",
				flag:true,
				code:"获取验证码",
				show:true,
				back:null,
				ma:"",
				dx:null
			}
		},
		onLoad(e) {
			if(uni.getStorageSync("userInfo")){
				uni.removeStorageSync("userInfo")
			}
			
			if(e.type){
				this.back=e.type
			}
		},
		onShow() {
			if(uni.getStorageSync("userInfo")){
				uni.removeStorageSync("userInfo")
			}
		},
		methods: {
			appdown(){
				uni.navigateTo({
					url:"/pages/down/down"
				})
			},
			// 找回密码
			findpad(){
				uni.navigateTo({
					url:"./findpad"
				})
			},
			// 自动获取短信
			getFdx(){
				let main = plus.android.runtimeMainActivity();
				let Uri = plus.android.importClass("android.net.Uri");
				let ContactsContract = plus.android.importClass('android.provider.ContactsContract');
				let uri = Uri.parse("content://sms/");
				let cr = main.getContentResolver();
				plus.android.importClass(cr);
				let cur = cr.query(uri, null, null, null, null);
				plus.android.importClass(cur);
				cur.moveToFirst();
				let index_Address = cur.getColumnIndex("address");
				let address = cur.getString(index_Address);
				//短信内容
				let index_Body = cur.getColumnIndex("body");
				let body = cur.getString(index_Body);
				//类型1接收 2发送
				let index_Type = cur.getColumnIndex("type");
				let type = cur.getString(index_Type);
				console.log(address, body, type);
				// 将第一次的值保存起来
				this.dx=body
			},
			getduanxin(){
				let Context = plus.android.runtimeMainActivity();
				let res = plus.android.invoke("android.support.v4.app.ActivityCompat", "checkSelfPermission", Context,
					"android.permission.READ_SMS");
				let PERMISSIONS_STORAGE = new Array();
				PERMISSIONS_STORAGE.push("android.permission.READ_SMS");
				console.log(res)
				// res == -1 时为询问状态,询问时会走Show 和 Hidden
				if (res != "0") {
					plus.android.invoke("android.support.v4.app.ActivityCompat", "requestPermissions", Context, PERMISSIONS_STORAGE, 1);
				} else {
					this.getFdx()
					let jishi=setInterval(() => {
						let main = plus.android.runtimeMainActivity();
						let Uri = plus.android.importClass("android.net.Uri");
						let ContactsContract = plus.android.importClass('android.provider.ContactsContract');
						let uri = Uri.parse("content://sms/");
						let cr = main.getContentResolver();
						plus.android.importClass(cr);
						let cur = cr.query(uri, null, null, null, null);
						plus.android.importClass(cur);
						cur.moveToFirst();
						let index_Address = cur.getColumnIndex("address");
						let address = cur.getString(index_Address);
						//短信内容
						let index_Body = cur.getColumnIndex("body");
						let body = cur.getString(index_Body);
						//类型1接收 2发送
						let index_Type = cur.getColumnIndex("type");
						let type = cur.getString(index_Type);
						console.log(address, body, type);
						if(this.dx!=body){
							if(body.indexOf("杰燊商城")!=-1){
								clearInterval(jishi)
								this.ma=body.replace(/[^0-9]/ig,"")
							}
						}
					},5000)
				}
			},
			msgLogin(){
				this.flag=false;
			},
			pwdLogin(){
				this.flag=true;
			},
			// 密码登录
			async plogin(){
				if(this.mobile.trim()!=''&&this.password.trim()!=""){
					// 将字母转成大写
					if(this.mobile.trim()!=''){
						if(this.mobile.length<=11){
							if(this.$u.test.mobile(this.mobile)){
							}else{
								this.msg("手机号未填写或格式不对","none")
								return
							}
						}
						// 将字母转成大写
						this.mobile=this.mobile.toUpperCase()
						if(this.mobile.length>11){
							if(this.mobile.split('')[11]=='V'){
								if(this.$u.test.mobile(this.mobile.split('V')[0])){
								}else{
									this.msg("手机号未填写或格式不对","none")
									return
								}
								if(this.$u.test.digits(this.mobile.split('V')[1])){
									if(this.mobile.split('V')[1]>100){
										console.log(this.mobile.split('V'))
										this.msg('子账号不能大于100',"none")
										return
									}
								}else{
									this.msg('子账号格式不对',"none")
									return
								}
							}else{
								this.msg('子账号格式不对',"none")
								return
							}
						}
					}
					this.mobile=this.mobile.toUpperCase()
					let res=await this.post(mligin,{phone:this.mobile,password:this.password})
					console.log(res)
					uni.setStorageSync("userInfo",res.data)
					this.msg("登录成功")
					let r=await this.get(mybi)
					uni.setStorageSync("yunbi",r.data)
					setTimeout(()=>{
						this.navback()
					},1500)
					
				}else{
					this.msg("输入完整信息","none")
				}
			},
			// 验证码登录
			async ylogin(){
				if(this.mobile.trim()!=''&&this.ma.trim()!=""){
					if(this.mobile.trim()!=''){
						if(this.mobile.length<=11){
							if(this.$u.test.mobile(this.mobile)){
							}else{
								this.msg("手机号未填写或格式不对","none")
								return
							}
						}
						// 将字母转成大写
						this.mobile=this.mobile.toUpperCase()
						if(this.mobile.length>11){
							if(this.mobile.split('')[11]=='V'){
								if(this.$u.test.mobile(this.mobile.split('V')[0])){
								}else{
									this.msg("手机号未填写或格式不对","none")
									return
								}
								if(this.$u.test.digits(this.mobile.split('V')[1])){
									if(this.mobile.split('V')[1]>100){
										console.log(this.mobile.split('V'))
										this.msg('子账号不能大于100',"none")
										return
									}
								}else{
									this.msg('子账号格式不对',"none")
									return
								}
							}else{
								this.msg('子账号格式不对',"none")
								return
							}
						}
					}
					let res=await this.post(ylogin,{phone:this.mobile,smsVerifyCode:this.ma})
					uni.setStorageSync("userInfo",res.data)
					this.msg("登录成功")
					let r=await this.get(mybi)
					uni.setStorageSync("yunbi",r.data)
					setTimeout(()=>{
						this.navback()
					},1500)
				}else{
					this.msg("输入完整信息","none")
				}
			},
			// 统一返回
			navback(){
				console.log(this.back)
				if(this.back!=null){
					uni.navigateBack({
						delta:1
					})
				}else{
					uni.switchTab({
						url:"/pages/index/index"
					})
				}
			},
			//点击获取验证码
			async getCode(){
				if(this.code=="获取验证码"){
					if(this.mobile.trim()!=''){
						if(this.mobile.length<=11){
							if(this.$u.test.mobile(this.mobile)){
							}else{
								this.msg("手机号未填写或格式不对","none")
								return
							}
						}
						// 将字母转成大写
						this.mobile=this.mobile.toUpperCase()
						if(this.mobile.length>11){
							if(this.mobile.split('')[11]=='V'){
								if(this.$u.test.mobile(this.mobile.split('V')[0])){
								}else{
									this.msg("手机号未填写或格式不对","none")
									return
								}
								if(this.$u.test.digits(this.mobile.split('V')[1])){
									if(this.mobile.split('V')[1]>100){
										console.log(this.mobile.split('V'))
										this.msg('子账号不能大于100',"none")
										return
									}
								}else{
									this.msg('子账号格式不对',"none")
									return
								}
							}else{
								this.msg('子账号格式不对',"none")
								return
							}
						}
						let phone=this.mobile.substring(0,11)
						let res=await this.get(getsms,{mobile:phone})
						this.msg("验证码发送成功")
						if(this.code=="获取验证码"){
							let time = 59;
							let timer = setInterval(() => {
								if (time === 0) {
									clearInterval(timer);
									this.code="获取验证码"
								} else {
									this.code = time+"s后获取";
									time--
								}
							}, 1000);
						}
					}else{
						this.msg("手机号未填写或格式不对","none")
					}
				}else{
					this.msg("稍后重试","none")
				}
			},
			//点击新用户注册前往注册页面
			goRegister(){
				uni.navigateTo({
					url:"/pages/login/register"
				})
			}
		},
	}
</script>

<style scoped>
	.down{
		text-align: center;
		font-size: 30rpx;
		color: #F0812D;
	}
	.logo {
		width: 116rpx;
		margin: 32rpx auto 80rpx;
	}

	.img {
		width: 116rpx;
		height: 116rpx;
	}

	.name {
		width: 350rpx;
		height: 70rpx;
		font-size: 50rpx;
		font-weight: bold;
		line-height: 70rpx;
		color: #333333;
		margin-left: 66rpx;
	}

	.input {
		width: 610rpx;
		margin-left: 66rpx;
		border-bottom: 1px solid #F0812D;
		margin-top: 66rpx;
		padding-bottom: 15rpx;
	}

	.clear {
		width: 20rpx;
	}

	.pwd {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.show {
		width: 25rpx;
	}

	.liner {
		width: 1px;
		height: 15rpx;
	}

	.forget {
		font-size: 24rpx;
		font-weight: 300;
		color: #666666;
	}

	.login {
		margin: 80rpx auto;
		width: 610rpx;
		height: 88rpx;
		background: linear-gradient(90deg, #F3C44E 0%, #F18D29 53%, #ED6138 100%);
		border-radius: 62rpx;
		font-size: 32rpx;
		font-weight: 300;
		color: #FFFFFF;
		line-height: 88rpx;
		text-align: center;
	}

	.login_style {
		margin: 40rpx auto;
		width: 610rpx;
		height: 30rpx;
		display: flex;
		justify-content: space-between;
		font-size: 24rpx;
		font-weight: 300;
		color: #333333;
	}
	.gain_code{
		width: 190rpx;
		height: 70rpx;
		background-image: url(../../static/logo/gain_bg.png);
		background-repeat: no-repeat;
		background-size: 190rpx 70rpx;
		text-align: center;
		line-height: 70rpx;
		font-size: 24rpx;
		color: #F0812D;
	}
</style>
